import util from '../../common/js/util'
import NProgress from 'nprogress'
import axios from 'axios';
import address from "../../../models/dict/address"
import moment from 'moment'
const py = require('../../../models/comm/webPy');
const dMember = require('../../../models/dict/dMember');

export default {
    data() {
        return {
            filters: {
                name: ''
            },
            memberList: [],
            total: 0,
            page: 1,
            listLoading: false,
            sels: [],//列表选中列

            editFormVisible: false,//编辑界面是否显示
            editLoading: false,
            editFormRules: {
                name: [
                    { required: true, message: '请输入姓名', trigger: 'blur' }
                ]
            },
            //编辑界面数据1
            editForm: {
                id: 0,
                name: '',
                sex: -1,
                age: 0,
                birth: '',
                addr: ''
            },

            addFormVisible: false,//新增界面是否显示
            addLoading: false,
            ShowSave: false,
            addFormRules: {
                name: [
                    { required: true, message: '请输入姓名', trigger: 'blur' }
                ]
            },
            //新增界面数据
            addForm: {
                name: '',
                sex: -1,
                age: 0,
                birth: '',
                addr: ''
            },
            currObj: {


                address: {}
            },
            addressOptions: address.address,
            selectedOptions: ["330000", "330100", "330110"],

            memberClassArray: [
                {

                    label: "设计师",
                    value: "10"

                },
                {
                    label: "其他",
                    value: "20"

                }

            ],
            v: [],
            msgContent: ""



        }
    },
    methods: {
        //性别显示转换
        sendMsg: function () {
            axios.post("/a", {
                class: "member",
                para: "sendMsg",
                j: {
                    content: this.msgContent,
                    toUserId: this.currObj._id

                }
            }).then((res) => {



            });

        },
        formatSex: function (row, column) {
            return row.sex == 1 ? '男' : row.sex == 0 ? '女' : '未知';
        },
        fmtDate: function (row, col) {

            return moment(row.createTime).format("YYYY-MM-DD HH:mm:ss");

        },
        fmtaddress: (row, col) => {
            if (row.address) {

                return row.address.provinceName + " " + row.address.cityName
            }
            else {

                return "未知"
            }

        },
        addressChange: (value) => {

            var iii = value;

        },

        handleCurrentChange(val) {
            this.page = val;
            this.getMemberList();
        },
        //获取用户列表
        getMemberList() {

            this.listLoading = true;
            NProgress.start();
            axios.post("/a", {
                class: "member",
                para: "getMemberList",
                j: {
                    data: {



                        companyCode: localStorage.companyCode


                    },
                    page: this.page


                }

            }).then((res) => {

                this.memberList = res.data.list;

                this.total = res.data.total;
                this.listLoading = false;
                NProgress.done();


            });
        },
        addMember() {

            this.currObj = {

                address: {},
                class: []
            }
            this.ShowSave = true;
        },

        //删除
        handleDel: function (index, row) {

        },
        //显示编辑界面
        toSaveMember: function (index, row) {
            axios.post("/a", {
                class: "member",
                para: "getMember",
                j: {
                    _id: this.memberList[index]._id
                }

            }).then((res) => {

                if (!res.data.info.class) {
                    res.data.info.class = [];


                }

                this.currObj = dMember.fmtMember(res.data.info);
                this.ShowSave = true;


                if (!this.currObj.address) {

                    this.currObj.address = {};
                    this.currObj.address.areaId = '';
                }

                if (!this.currObj.class) {

                    this.currObj.class = [];
                }
 
                   this.selectedOptions = [this.currObj.address.provinceId, this.currObj.address.cityId, this.currObj.address.areaId]
            });

        },
        //保存客户
        saveMember: function () {

             this.currObj.address = address.addressObj(this.selectedOptions, this.currObj.address.memo);
            this.currObj.companyCode = localStorage.companyCode;



            axios.post("/a", {
                class: "member",
                para: "saveMember",
                j: this.currObj

            }).then((res) => {
                if (res.data.re == "ok") {
                    this.ShowSave = false;
                    this.getMemberList();
                } else {
                    alert(JSON.stringify(res.data));
                }
            });


        },
        //新增
        querySearchRole: async function (query, cb) {
            var data = {
                "$or": [
                    { name: { $regex: query } },
                    { namePy: { $regex: await py.firstLetter(query) } }
                ]
            }
            //  console.log(JSON.stringify(data));
            if (query !== '') {
                this.loading = true;
                axios.post("/a", {
                    class: "member",
                    para: "getRoleList",
                    j: {
                        data: data
                    }

                }).then((res) => {

                    this.loading = false;

                    var list = res.data.list;

                    list.forEach(function (role) {
                        role.value = role.name;

                    }, this);


                    cb(list)

                });

            }
        },

        selRole(role) {
            var has = false;
            this.currObj.roleArray.forEach(function (roleItem) {

                if (role._id == roleItem._id) {
                    has = true
                }

            }, this);
            if (!has) {
                this.currObj.roleArray.push({ _id: role._id, name: role.name })
            }


        },
        BindRole() {

        },
        removeRole(index) {
            this.currObj.roleArray.splice(index, 1);
        }


    },


    mounted() {
        this.getMemberList();
    },

}